/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-08-09 08:57:57
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-09-27 11:15:11
 */
import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { CreateOrEditBopModalComponent } from '@app/admin/bop/create-or-edit-bop-modal.component';
import { Paginator } from 'primeng/components/paginator/paginator';
import { QualityPlatformServiceProxy, BopUnifyServiceProxy ,BopNodeServiceProxy,} from '@shared/service-proxies/service-proxies';
import { Table } from 'primeng/components/table/table';
import { finalize } from 'rxjs/operators';
import { ShowResumeComponent } from '../components/show-resume/show-resume.component'
import { BopManageComponent } from '../components/bop-manage/bop-manage.component';
import { JsUtilsApi } from 'great-jsutils';
import { MessageService } from '@abp/message/message.service';
import { TskExportComponent } from '../components/tsk-export/tsk-export.component';

@Component({
    selector: 'app-packet',
    templateUrl: './packet.component.html',
    styleUrls: ['./packet.component.less']
})
export class QmPacketQueryComponent extends AppComponentBase {
    @ViewChild('createOrEditBopManageModal') createOrEditBopManageModal: BopManageComponent;
    @ViewChild('TskExportComponent') TskExportComponent: TskExportComponent;
    // @ViewChild('createOrEditBopModal') createOrEditBopModal: CreateOrEditBopModalComponent;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('showResume') ShowResume: ShowResumeComponent;

    filterText = '';
    //单行数据
    rowdata: any;
    //表单数据
    model: string = '';
    lotNo: string = '';
    drawingNo: string = '';
    uploadedFiles = [];
    rangeFiles = [];
    packetFiled = [];
    fileMd5 = undefined;
    saveFlag = true;
    exportInTitle = undefined;
    exportIn = false;
    fileName = undefined;
    constructor(injector: Injector,
        private qualityPlatform: QualityPlatformServiceProxy,
        private _messageService: MessageService,
        private _bopUnifyServiceProxy: BopUnifyServiceProxy,
        private _bopNodeServiceProxy: BopNodeServiceProxy,
    ) {
        super(injector);
    }
     // 弹窗隐藏
     displayclose() { 
         this.fileName = undefined;
     }
     openDialog(type){
        if (type == 1) {
            this.exportInTitle = '5G数据包导入';
        }else{
            this.exportInTitle = '靶场数据包导入';
        }
        this.exportIn = true;
     }
     alterCertificateByBusinessEv(){
         if (this.exportInTitle == '5G数据包导入') {
             abp.ui.setBusy();
             this._bopUnifyServiceProxy.uploadBigBop(
               this.fileName
            )
                .pipe(finalize(() => { }))
                .subscribe(result => {
                    this.saveFlag = true;
                    abp.ui.clearBusy();
                    this.fileName = undefined;
                    if (result.code == 0) {
                        this._messageService.success('导入成功');
                        this.getBopList();
                    } else {
                        this._messageService.error(result.message);
                    }
                });
         }else{
             abp.ui.setBusy();
             this.qualityPlatform.rShootingBigRangeBop(
               this.fileName
            )
                .pipe(finalize(() => { }))
                .subscribe(result => {
                    this.saveFlag = true;
                    abp.ui.clearBusy();
                    this.fileName = undefined;
                    if (result.code == 0) {
                        this._messageService.success('导入成功');
                        this.getBopList();
                    } else {
                        this._messageService.error(result.message);
                    }
                });
         }
        
     }
    myUploader(event) {
        // let file = event.files[0];
        let data = { data: event.files[0], fileName: event.files[0].name };
        // JsUtilsApi.md5File(file).then(
        //     (md5) => {
        //         this.fileMd5 = md5;
        //         this.fileMd5 = this.fileMd5.toLowerCase();
                abp.ui.setBusy();
                this.saveFlag = false;
                this._bopUnifyServiceProxy.uploadBigBop(
                    event.files[0].name
                    // this.fileMd5
                )
                    .pipe(finalize(() => { }))
                    .subscribe(result => {
                        this.saveFlag = true;
                        abp.ui.clearBusy();
                        this.fileMd5 = undefined;
                        this.uploadedFiles = [];
                        if (result.code == 0) {
                            this._messageService.success('导入成功');
                            this.getBopList();
                        } else {
                            this._messageService.error(result.message);
                        }
                    });

        //     },
        //     (error) => {
        //         console.log(error);
        //     }
        // );
    }
    placeOnFile(row){
        this.message.confirm(
            '确认将这条数据归档吗',
            (isConfirmed) => {
                if (isConfirmed) {
                    abp.ui.setBusy();
                    if (this.saveFlag) {
                        this.saveFlag = false;
                        this.qualityPlatform.placeOnFile(row.id).pipe(finalize(() => { }))
                            .subscribe(result => {
                                abp.ui.clearBusy();
                                this.saveFlag = true;
                                if (result.code == 0) {
                                    this._messageService.success(result.message);
                                    this.dowloadPlace(row.id);
                                    this.getBopList();
                                } else {
                                    this._messageService.error(result.message);
                                }
                            });
                    }
                }
            }
        );
    }
    dowloadPlace(id){
        this.qualityPlatform.placeOnFileDonland(id).pipe(finalize(() => { }))
        .subscribe(result => {
            if (result.code == 0) {
                window.location.href = result.message;
            } else {
                this._messageService.error(result.message);
            }
        })
    }
    rangeUploader(event) {
        // let file = event.files[0];
        let data = { data: event.files[0], fileName: event.files[0].name };

        // JsUtilsApi.md5File(file).then(
        //     (md5) => {
        //         this.fileMd5 = md5;
        //         this.fileMd5 = this.fileMd5.toLowerCase();
                abp.ui.setBusy();
                this.saveFlag = false;
                this.qualityPlatform.rShootingBigRangeBop(
                    event.files[0].name
                    // this.fileMd5
                )
                    .pipe(finalize(() => { }))
                    .subscribe(result => {
                        this.saveFlag = true;
                        abp.ui.clearBusy();
                        this.packetFiled = [];
                        this.fileMd5 = undefined;
                        if (result.code == 0) {
                            this._messageService.success('导入成功');
                            this.getBopList();
                        } else {
                            this._messageService.error(result.message);
                        }
                    });
        //     },
        //     (error) => {
        //         console.log(error);
        //     }
        // );
    }
    rangeExport(row) {
        this.message.confirm(
            '确认导出这条数据吗',
            (isConfirmed) => {
                if (isConfirmed) {
                    abp.ui.setBusy();
                    if (this.saveFlag) {
                        this.saveFlag = false;
                        this.qualityPlatform.getrShootingRange(row.id).pipe(finalize(() => { }))
                            .subscribe(result => {
                                abp.ui.clearBusy();
                                this.saveFlag = true;
                                if (result.code == 0) {
                                    window.location.href = result.message;
                                } else {
                                    this._messageService.error(result.message)
                                }
                            })
                    }
                }
            }
        );
    }
    exportPhoto(row) {
        setTimeout(() => {
            this.rowdata = row;
            this.TskExportComponent.show();
        }, 10);
        // this.message.confirm(
        //     '确认导出这条数据吗',
        //     (isConfirmed) => {
        //         if (isConfirmed) {
        //             abp.ui.setBusy();
        //             if (this.saveFlag) {
        //                 this.saveFlag = false;
        //                 this.qualityPlatform.getDataPackage(row.id).pipe(finalize(() => { }))
        //                     .subscribe(result => {
        //                         abp.ui.clearBusy();
        //                         this.saveFlag = true;
        //                         if (result.code == 0) {
        //                             window.location.href = result.message;
        //                         } else {
        //                             this._messageService.error(result.message)
        //                         }
        //                     })
        //             }
        //         }
        //     }
        // );
    }
    //分页页面切换事件
    onPageChange(event?: LazyLoadEvent) {

    }
    //打开bop树
    toBopTree(row): void {
        this.rowdata = row;
        let that = this;
        setTimeout(() => {
            that.createOrEditBopManageModal.show();
            that.createOrEditBopManageModal.getTreeInfo();
        }, 10);

    }
    //查看履历
    lookResume(row): void {
        let that = this
        this.rowdata = row
        setTimeout(() => {
            that.ShowResume.show()
            that.ShowResume.getTableList()
        }, 20)
    }
    //获取主页列表
    getBopList(event?: LazyLoadEvent): void {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this.qualityPlatform.getQualityTaskList(
            this.filterText,
            this.drawingNo,
            this.model,
            this.lotNo,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        })
    }
    //重置搜索
    resetSearch(): void {
        this.model = ''
        this.lotNo = '',
            this.drawingNo = ''
        this.getBopList()
    }
    ngOnInit(): void {
    }
}
